package com.dq.service.sys.user.centre.impl;

import com.dq.controller.in.user.UserPlanQuery;
import com.dq.domain.org.Org;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.sys.user.user.BossUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.centre.*;
import com.dq.exception.customException.AdminException;
import com.dq.repository.BaseRepositories;
import com.dq.service.sys.user.BossUserService;
import com.dq.service.sys.user.centre.UserPlanService;
import com.dq.utils.BeanNullUtil;
import com.google.gson.Gson;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.*;

/**
 * @author liuhui
 * @date 2019/12/2 0002 14:34:06
 */
@Service
public class UserPlanServiceImpl extends BaseRepositories implements UserPlanService {
    @Autowired
    private BossUserService bossUserService;

    @Override
    public List<UserPlan> list(UserPlanQuery userPlanQuery) throws AdminException {
        Map<String, Object> query = createQuery(userPlanQuery);

        return dao.find(dao.query2Hql(UserPlan.class, query), query);
    }
    @Override
    public Long count(UserPlanQuery userPlanQuery) throws AdminException {
        Map<String, Object> query = createQuery(userPlanQuery);

        return dao.count(dao.count2Hql(UserPlan.class, query), query);
    }
    public Map<String, Object> createQuery(UserPlanQuery userPlanQuery){
        Map<String, Object> query = userPlanQuery.parseMap();
        if(userPlanQuery.userId != null) {
            query.put("bossUser_id", userPlanQuery.getUserId());
            query.remove("userId");
        }

        return query;
    }


    @Override
    public UserPlan save(@Valid UserPlan userPlan) {
        if(userPlan.getBossUserId() == null) {
            throw new AdminException("未关联员工");
        }
        BossUser bossUser = dao.get(BossUser.class, userPlan.getBossUserId());
        if(bossUser == null) {
            throw new AdminException("无对应员工, id: " + userPlan.getBossUserId());
        }

        userPlan.setBossUser(bossUser);
        if(bossUser.getSection() == null || !(bossUser.getSection() instanceof Org)) {
            throw new AdminException("此员工所属门店信息异常");
        }
        userPlan.setSection(bossUser.getSection());

        verifyObject(userPlan);

        SysUser loginUser = sysUserService.getUser();
        userPlan.setCreateUser(loginUser);
        userPlan.setUpdateUser(loginUser);

        userPlan = userPlanRepository.save(userPlan);
        return userPlan;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public UserPlan update(UserPlan userPlan) throws AdminException {
        UserPlan get = dao.get(UserPlan.class, userPlan.getId());
        if(get == null) {
            throw new AdminException("编辑对象异常");
        }
        Gson gson = bossUserService.getGson();
        UserPlan update = gson.fromJson(gson.toJson(get), UserPlan.class);
        BeanUtils.copyProperties(userPlan, update, BeanNullUtil.getNullPropertyNames(userPlan));

        verifyObject(update);

        SysUser loginUser = sysUserService.getUser();
        update.setCreateUser(get.getCreateUser());
        update.setUpdateUser(loginUser);

        update = userPlanRepository.save(update);

        return update;
    }


    public void verifyObject(UserPlan userPlan) throws AdminException {
        if (userPlan.getSection() == null) {
            throw new AdminException("未关联门店");
        }
        if (userPlan.getBossUser() == null) {
            throw new AdminException("未关联员工");
        }

        UserPlan get = userPlanRepository.findByDeletedFalseAndYearAndMonthAndBossUser(userPlan.getYear(), userPlan.getMonth(), userPlan.getBossUser());
        if(get != null) {
            if (userPlan.getId() == null) {
                throw new AdminException("对应日期已有工作计划");
            }else {
                if(!userPlan.getId().toString().equals(get.getId().toString())) {
                    throw new AdminException("对应日期已有工作计划");
                }
            }
        }
    }
}
